import axios from 'axios'
import qs from 'qs'
import { httpGet, httpPost } from '@/utils/http'

// 创建一个包含默认配置的axios实例
const axiosInstance = axios.create({
  withCredentials: true, // 确保跨域请求时携带cookie
  headers: {
    'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest'
  }
})

// 配置qs序列化选项，确保中文正确编码
const qsOptions = {
  charset: 'utf-8',
  arrayFormat: 'brackets'
}

/** 获取酒店列表 */
export const getHotelList = (params: object) => {
    return axiosInstance.get('/api/hotel/listAll.do', { params })
}
/** 添加酒店 */
export const createHotel = (params: object) => {
    console.log('发送添加酒店请求，参数:', params);
    return axiosInstance.post('/api/hotel/add.do', qs.stringify(params, qsOptions), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    })
}
/** 更新酒店 */
export const updateHotel = (params: object) => {
    console.log('发送更新酒店请求，参数:', params);
    return axiosInstance.post('/api/hotel/update.do', qs.stringify(params, qsOptions), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    })
}
/** 删除酒店 */
export const deleteHotel = (hotelName: string) => {
    console.log('发送删除酒店请求，参数:', { hotelName });
    return axiosInstance.post('/api/hotel/delete.do', qs.stringify({ hotelName }, qsOptions), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    })
}
/** 根据名称获取酒店 */
export const getHotelByName = (hotelName: string) => {
    return httpGet('/api/hotel/get.do', { hotelName })
}
/** 根据位置筛选酒店 */
export const getHotelsByLocation = (params: { country: string; city: string }) => {
    return axiosInstance.get('/api/hotel/listByLocation.do', { params })
}

/** 根据用户ID获取酒店信息 */
export const getHotelByUserId = (userId?: string) => {
    return axiosInstance.get('/api/hotel/getByUserId.do', { 
        params: userId ? { userId } : {} 
    })
}
